<?php
class Upload{
	private $path;
	private $max_size;
	private $errno;
	private $mime = array('image/jpeg','image/png','image/gif');

	public function __construct(){
		$this->path = PUBLIC_PATH . 'Upload' . DS;
		$this->max_size = 1000000;
	}
	
	public function up($file){
		if ($file['error'] == 0) {
			if (!in_array($file['type'], $this->mime)) {
				$this->errno = -1; 
				return false;
			}

			if ($file['size'] > $this->max_size) {
				$this->errno = -2;
				return false;
			}

			$sub_path = date('Ymd'). DS;
			if (!is_dir($this->path . $sub_path)) {
				mkdir($this->path . $sub_path);
			}
			
			$file_name = date('YmdHis').uniqid().strrchr($file['name'], '.');

			if (move_uploaded_file($file['tmp_name'], $this->path . $sub_path . $file_name)) {
				return $sub_path . $file_name;
			} else {
				$this->errno = -3;
				return false;
			}

		} else {
			$this->errno = $file['error'];
			return false;
		}
		
	}

	public function error(){
		switch ($this->errno) {
			case -1:
				return '请检查你的文件类型，目前支持的类型有'.implode(',', $this->mime);
				break;
			case -2:
				return '文件超出系统规定的大小，最大不能超过'. $this->max_size;
				break;
			case -3:
				return '文件移动失败';
				break;
			case 1:
				return '上传的文件超过了 php.ini 中 upload_max_filesize 选项限制的值,其大小为'.ini_get('upload_max_filesize');
				break;
			case 2:
				return '上传文件的大小超过了 HTML 表单中 MAX_FILE_SIZE 选项指定的值,其大小为' . $_POST['MAX_FILE_SIZE'];
				break;
			case 3:
				return '文件只有部分被上传';
				break;
			case 4:
				return '没有文件被上传';
				break;
			case 5:
				return '非法上传';
				break;
			case 6:
				return '找不到临时文件夹';
				break;
			case 7:
				return '文件写入临时文件夹失败';
				break;
			default:
				return '未知错误,灵异事件';
				break;
		}
	}
}